﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.service;
using DevExpress.Data;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.UC.款式异常;
using DXApplicationTangche.UC.门店下单.form.订单修改;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DiaoPaiDaYin;
using DXApplicationTangche.model;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DXApplicationTangche.UC.订单.service;
using System.IO;
using DXApplicationTangche.UC.门店下单.service;
using DXApplicationTangche.UC.销售台账.service;

namespace DXApplicationTangche.UC.门店下单.form.工厂订单一览
{
    public partial class Frm坚山订单一览 : DevExpress.XtraEditors.XtraForm
    {
        public Enum订单类别 enum订单类别 = Enum订单类别.坚山订单;
        
        public Frm坚山订单一览(Enum订单类别 @enum)
        {
            this.enum订单类别 = @enum;
            InitializeComponent();
            if (this.enum订单类别 == Enum订单类别.坚山美国订单)
            {
                this.layoutControlItemCSV按钮.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                this.simpleButton坚山csv更新地址.Enabled = true;
            }
        }
        private void Frm已付款订单一览_Load(object sender, EventArgs e)
        {
            if (this.enum订单类别 == Enum订单类别.坚山订单)
            {
                this.gridControl订单一览.DataSource = 订单Service.getKashiyamaOrderStatusIndexPageable("46");
            }
            else if (this.enum订单类别 == Enum订单类别.坚山上海订单)
            {
                this.gridControl订单一览.DataSource = 订单Service.getKashiyamaOrderStatusIndexPageable("52");
            }
            else if (this.enum订单类别 == Enum订单类别.坚山美国订单)
            {
                this.gridControl订单一览.DataSource = 订单Service.getKashiyamaOrderStatusIndexPageable("49");
            }
            this.gridView1.Columns["orderDate"].SortOrder = ColumnSortOrder.Descending;
            this.gridView1.ExpandAllGroups();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            String orderId = FunctionHelper.GetgridViewValue(this.gridView1, e.RowHandle, "orderId").ToString();
            new Form订单预览新(orderId).ShowDialog();
            //if (this.enum订单类别 == Enum订单类别.坚山订单)
            //{
            //    new Form订单预览新(
            //        FunctionHelper.GetgridViewValue(
            //            this.gridView1, e.RowHandle, "ORDER_ID")).ShowDialog();
            //}
            //else if (this.enum订单类别 == Enum订单类别.坚山上海订单)
            //{
            //    new Form订单预览新(
            //        FunctionHelper.GetgridViewValue(
            //            this.gridView1, e.RowHandle, "ORDER_ID")).ShowDialog();
            //}
            //else if (this.enum订单类别 == Enum订单类别.坚山美国订单)
            //{
            //    // 这部分是20210315改动
            //    List<尺寸呈现dto> lst = new List<尺寸呈现dto>();
            //    try
            //    {
            //        lst = SizeService.getDto尺寸ByOrderId(
            //                this.gridView1.GetDataRow(e.RowHandle)["ORDER_ID"].ToString()
            //                , this.gridView1.GetDataRow(e.RowHandle)["STYLE_FIT_CD"].ToString()
            //                , this.gridView1.GetDataRow(e.RowHandle)["STYLE_CATEGORY_CD"].ToString()
            //                , this.gridView1.GetDataRow(e.RowHandle)["STYLE_SIZE_CD"].ToString()
            //                , this.gridView1.GetDataRow(e.RowHandle)["STYLE_SIZE_GROUP_CD"].ToString()
            //                , this.gridView1.GetDataRow(e.RowHandle)["CUSTOMER_ID"].ToString()
            //                );
            //    }
            //    catch { }
            //    List<InspectSizeDTO> inspectSizeDTOs = new List<InspectSizeDTO>();
            //    inspectSizeDTOs = FunctionHelper.JsonDeserialization<List<InspectSizeDTO>>(RestCall.httpGetMethod(URL.接口url + "/barcode/getMeasureItemFacadeByOrderId?orderId=" + this.gridView1.GetDataRow(e.RowHandle)["ORDER_ID"].ToString()));
            //    if (inspectSizeDTOs.Count == 0 || inspectSizeDTOs == null)
            //    {
            //    }
            //    else
            //    {
            //        lst.Clear();
            //        foreach (InspectSizeDTO inspectSizeDTO in inspectSizeDTOs)
            //        {
            //            lst.Add(new 尺寸呈现dto(inspectSizeDTO));
            //        }
            //    }
            //    String jsonlst = FunctionHelper.JsonSerialization(lst);
            //    new Frm订单预览(
            //                    this.gridView1.GetDataRow(e.RowHandle)["STYLE_ID"].ToString()
            //                    , lst
            //                    , this.gridView1.GetDataRow(e.RowHandle)["ORDER_ID"].ToString()
            //                    , this.gridView1.GetDataRow(e.RowHandle)["REMARKS"].ToString()
            //                    ,Enum订单类别.坚山美国订单
            //                    ).HideFor任务().ShowDialog();
            //}
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void refreshData()
        {
            if (this.enum订单类别 == Enum订单类别.坚山订单)
            {
                this.gridControl订单一览.DataSource = 订单Service.getKashiyamaOrderStatusIndexPageable("46");
            }
            else if (this.enum订单类别 == Enum订单类别.坚山上海订单)
            {
                this.gridControl订单一览.DataSource = 订单Service.getKashiyamaOrderStatusIndexPageable("52");
            }
            else if (this.enum订单类别 == Enum订单类别.坚山美国订单)
            {
                this.gridControl订单一览.DataSource = 订单Service.getKashiyamaOrderStatusIndexPageable("49");
            }
            this.gridView1.Columns["orderDate"].SortOrder = ColumnSortOrder.Descending;
            this.gridView1.RefreshData();
            this.gridView1.ExpandAllGroups();
        }

        #region 选择门店
        //private String status_code;
        //private String status_name;
        //private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        //{
        //    //得到当前SearchLookUpEdit弹出窗体
        //    PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
        //    SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
        //    LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
        //    //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
        //    if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
        //    {
        //        //得到空的空间
        //        EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

        //        ////得到取消按钮，重写点击事件
        //        //Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
        //        //LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
        //        //clearBtn.Click += clearBtn_Click;

        //        //添加一个simplebutton控件(确认按钮)
        //        //LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
        //        //myLCI.TextVisible = false;
        //    }
        //}

        //private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        //{
        //    var a = ((SearchLookUpEdit)sender).Properties.View.GetSelectedRows();
        //    foreach (int rowHandle in a)
        //    {
        //        try
        //        {
        //            this.status_code //  no
        //                = ((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "code").ToString();//id 是 Value Member
        //        }
        //        catch { }

        //        this.status_name //  no
        //            = ((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "name").ToString();//id 是 Value Member
        //    }
        //    this.refreshData();
        //}

        ///// <summary>
        ///// 清除按钮事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void clearBtn_Click(object sender, EventArgs e)
        //{
        //    this.searchLookUpEdit1.ToolTip = null;
        //    this.searchLookUpEdit1.EditValue = null;
        //}
        //private void searchLookUpEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        //{
        //    if (null != e.Value)
        //        e.DisplayText = this.status_name;
        //}
        #endregion

        private void 已付款ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "orderId").ToString();
                订单Service.orderPayment(orderId, true);
                MessageBox.Show("已改为已付款");
                this.refreshData();
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void 待付款ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "orderId").ToString();
                订单Service.orderPayment(orderId, false);
                MessageBox.Show("已改为待付款");
                this.refreshData();
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void 取消订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "orderId").ToString();
                DTO返回值判断 dTO返回值判断 = 订单Service.get清单取消判断(orderId);
                if ("200".Equals(dTO返回值判断.errorCode))
                {
                    RestCall.PostHttp(
                        URL.接口url + "/pmc/orderCancel", 
                        gridView1.GetRowCellValue(
                            this.gridView1.FocusedRowHandle, "orderId").ToString()
                            );
                    MessageBox.Show("已改为取消订单");
                    this.refreshData();
                }
                else
                {
                    MessageBox.Show(dTO返回值判断.errorMessage);
                }
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        #region 坚山csv更新地址
        private void simpleButton坚山csv更新地址_Click(object sender, EventArgs e)
        {
            this.updateAddressInfoByCSV();
            this.refreshData();
        }
        private void updateAddressInfoByCSV()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "csv|*.csv";
            ofd.Title = "请选择csv文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                DataTable dt = this.readCsvTxt(Path.GetFullPath(ofd.FileName));
                List<KashiyamaCSV> kashiyamaCSVs = new List<KashiyamaCSV>();
                foreach (DataRow dr in dt.Rows)
                {
                    kashiyamaCSVs.Add(new KashiyamaCSV(dr));
                }
                //FunctionHelper.JsonSerialization(kashiyamaCSVs);
                try
                {
                    List<OrderStatusSaleIndexDTO> list = 下单相关Service.updateAddressInfoByCSV(kashiyamaCSVs);
                    //FunctionHelper.JsonSerialization(list);
                    if (list.Count <= 0)
                    {
                        MessageBox.Show("坚山csv更新地址失败");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("坚山csv更新地址: "+ex.Message);
                    MessageBox.Show("坚山csv更新地址失败");
                }
                MessageBox.Show("坚山csv更新地址成功");
            }
        }

        private DataTable readCsvTxt(string strpath)
        {
            int intColCount = 0;
            bool blnFlag = true;
            DataTable mydt = new DataTable("myTableName");
            DataColumn mydc;
            DataRow mydr;
            string strline;
            string[] aryline;
            System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);
            while ((strline = mysr.ReadLine()) != null)
            {
                aryline = strline.Split(',');
                if (blnFlag)
                {
                    blnFlag = false;
                    intColCount = aryline.Length;
                    for (int i = 0; i < aryline.Length; i++)
                    {
                        mydc = new DataColumn(aryline[i]);
                        mydt.Columns.Add(mydc);
                    }
                }
                mydr = mydt.NewRow();
                for (int i = 0; i < intColCount; i++)
                {
                    mydr[i] = aryline[i];
                }
                mydt.Rows.Add(mydr);
            }
            return mydt;
        }
        #endregion

        
    }
}
